import logging as log
import re
from typing import Any, Iterable, Mapping

import google.generativeai as genai
import google.generativeai.types.content_types as content_types
from google.generativeai.generative_models import GenerativeModel



class GeminiAgent:

    def __init__(self,model:GenerativeModel) -> None:
        self._model = model
        self._generationConfig = None

    def send_message(self,prompt:content_types.ContentType):
        chat = self._model.start_chat()
        response = chat.send_message(
            content=prompt,
            stream=True)
        return response
    
    def extract_python_script(self,markdown_text:str):
        """从markdown文档中提取python代码块"""
        # 正则表达式模式，用于匹配Markdown代码块
        python_code_block_pattern = r'```python\n(.*?)\n```'
        matches = re.findall(python_code_block_pattern, 
                            markdown_text, 
                            re.DOTALL | re.MULTILINE)
        python_script=""
        for match in matches:
            python_script += match.strip()
        if len(python_script)==0:
            raise ValueError("markdown文档中没有python脚本。")
        log.debug(f"从markdown文档中提取的python代码块：{python_script}")
        return python_script